package com.joyance.test_servlet.servlet;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.CollectionUtils;

public class TestSevlet extends HttpServlet {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;


    @SuppressWarnings("rawtypes")
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        // ServletConfig
        System.out.println("ServletConfig================");
        ServletConfig config = super.getServletConfig();
        // getServletName
        System.out.println("config.getServletName()=" + config.getServletName());
        // getInitParameter
        String realname = config.getInitParameter("realname");
        System.out.println("servletConfig.getInitParameter [realname=" + realname + "]");
        // getInitParameterNames
        Enumeration e = config.getInitParameterNames();
        System.out.print("servletConfig.getInitParameterNames [");
        while (e.hasMoreElements()) {
            Object name = e.nextElement();
            String value = config.getInitParameter(name.toString());
            System.out.print(name + "=" + value + ",");
        }
        System.out.print("]");

        // ServletContext
        System.out.println("\nServletContext================");
        ServletContext servletContext = super.getServletContext();
        // getAttribute
        Object username = servletContext.getAttribute("username");
        System.out.println("request.getAttribute(\"username\")=" + req.getAttribute("username"));
        System.out.println("servletContext.getAttribute(\"username\")=" + username);
        // getContext
        ServletContext sc = servletContext.getContext("/test_servlet/forward");
        Object password = sc.getAttribute("password");
        System.out.println("servletContext.getContext(\"/test_servlet/forward\").getAttribute(\"password\")=" + password);
        // getMajorVersion
        System.out.println("servletContext.getMajorVersion()=" + sc.getMajorVersion());
        // getMinorVersion
        System.out.println("servletContext.getMinorVersion()=" + sc.getMinorVersion());
        // getMimeType
        ServletContext context = getServletConfig().getServletContext();
        String mimeType = context.getMimeType("/WEB-INF/web.xml");
        System.out.println("servletContext.getMimeType()=" + mimeType);
        // getRealPath
        String realPath = sc.getRealPath("test");
        System.out.println("servletContext.getRealPath()=" + realPath);
        // getResource
        URL url = sc.getResource("index.html");
        System.out.println("servletContext.getResource()=" + url.getPath());
        // getResourceAsStream
        InputStream inputStream = sc.getResourceAsStream("index.html");
        byte[] bytes = new byte[1024];
        List<Byte> list = new ArrayList<Byte>();
        while (inputStream.read(bytes) != -1) {
            CollectionUtils.mergeArrayIntoCollection(bytes, list);
        }
        String str = new String(convertListToArray(list));
        System.out.println("sc.getResourceAsStream(\"index.html\"):\n" + str);
        // getServerInfo
        String servletInfo = sc.getServerInfo();
        System.out.println("sc.getServerInfo()=" + servletInfo);
        // log
        sc.log("==========++++++++++");
        // getInitParameter
        String globalName = sc.getInitParameter("globalName");
        System.out.println("sc.getInitParameter(\"globalName\")=" + globalName);

        // ServletRequest
        System.out.println("\nServletRequest================");
        Enumeration attNames = req.getAttributeNames();
        System.out.println("req.getAttributeNames[");
        while (attNames.hasMoreElements()) {
            Object attrName = attNames.nextElement();
            Object attrValue = req.getAttribute(attrName.toString());
            System.out.println(attrName + "=" + attrValue + ",");
        }
        System.out.println("]");
        // getCharacterEncoding
        System.out.println("req.getCharacterEncoding()=" + req.getCharacterEncoding());
        // getContentLength
        System.out.println("req.getContentLength()=" + req.getContentLength());
        // getContentType
        System.out.println("req.getContentType()=" + req.getContentType());
        // getProtocol
        System.out.println("req.getProtocol()=" + req.getProtocol());
        // getReader
        System.out.println("req.getReader()-------------------begin");
        // BufferedReader bufferedReader = req.getReader();
        // String line = bufferedReader.readLine();
        // while (line != null) {
        // System.out.println(line);
        // line = bufferedReader.readLine();
        // }
        System.out.println("req.getReader()-------------------end");
        // getInputStream
        System.out.println("req.getInputStream()-------------------begin");
        inputStream = req.getInputStream();
        bytes = new byte[1024];
        list = new ArrayList<Byte>();
        while (inputStream.read(bytes) != -1) {
            CollectionUtils.mergeArrayIntoCollection(bytes, list);
        }
        bytes = convertListToArray(list);
        String content = new String(bytes);
        System.out.println(content);
        System.out.println("req.getInputStream()-------------------end");
        // getRemoteAddr
        System.out.println("req.getRemoteAddr()=" + req.getRemoteAddr());
        // getRemoteHost
        System.out.println("req.getRemoteHost()=" + req.getRemoteHost());
        // getScheme
        System.out.println("req.getScheme()=" + req.getScheme());
        // getServerName
        System.out.println("req.getServerName()=" + req.getServerName());
        // getServerPort
        System.out.println("req.getServerPort()=" + req.getServerPort());
        // getRealPath
        System.out.println("req.getRealPath()=" + req.getRealPath("test"));

    }


    private byte[] convertListToArray(List<Byte> list) {
        byte[] bytes = new byte[list.size()];
        int index = 0;
        for (Byte b : list) {
            bytes[index++] = b;
        }
        return bytes;
    }


    public static void main(String[] args) {

        // create a new ResourceBundle with specified locale
        ResourceBundle bundle = ResourceBundle.getBundle("testBundle", Locale.US);

        // print the text assigned to key "hello"
        System.out.println("" + bundle.getString("hello"));

    }
}
